;-------------------------------------------------------------------------
; FILEIO: het leren beheren van files
;-------------------------------------------------------------------------
; Dit programma is nog in ontwikkeling

.MODEL small
.STACK 1024
;--- Macros 
MPrintString macro offsetstring
	push ax
	push dx
	mov ah,09h
		mov dx,offsetstring
	int 21h
	pop dx
	pop ax
endm


MOpenFile macro offsetfilename,operation
	; save context
	push ax
	push cx
	push dx
	MPrintString offset openmsg  
	; Open a specified file with dos interrupt	
	mov ah,fileopen
		mov al,operation		; ax = operation, 
								; 0:read only
								; 1:write only
								; 2:write and read
		mov dx,offsetfilename	; dx = startadress of file
	int 21h
	;restore context
	pop dx
	pop cx
	pop ax
endm

MCreateFile macro offsetfilename,attribute
	; save context
	push ax
	push cx
	push dx
	MPrintString offset createmsg
	; Create a specified file with dos interrupt	
	mov ah,filecreate
		mov cx,attribute		; cx = attribute, 
								;0 File is a Read-Only file
								;1 File is a hidden file
								;2 File is a system file
								;3 File is a volume label name
								;4 File is a subdirectory
								;5 File has been archived
		mov dx,offsetfilename 	; dx = startadress of file
	int 21h
	;restore context
	pop dx
	pop cx
	pop ax
endm

.DATA
; --- Constants
	filecreate = 3Ch
	fileopen = 3Dh
	fileread = 3Fh
	filewrite = 40h
	fileclose = 3Eh
	fileseek = 42h
	
; --- Variables	
	; messages
	openmsg 	db "Opening file: $" 
	closemsg	db "Closing file: $"
	readmsg		db "Reading file: $"
	seekmsg		db "seekig  file: $"
	createmsg	db "Creating file: $"
	succesmsg  	db "succes$"
	failuremsg 	db "failure$"
	
	; actual variables
	filename db "TEST.TXT",0
	array db 3 dup(?),'$'
	filehandler dw ? 
	endl db 0Ah,0Dh,"$"
	
.CODE
.STARTUP
MOpenFile offset filename,0
	jnc succes1
	MPrintString offset failuremsg
	jmp failure1
succes1:
	mov filehandler,ax
	MPrintString offset succesmsg
	jmp mainend
failure1:
	MPrintString offset endl
	
MCreateFile offset filename,0
	jnc succes1
	MPrintString offset failuremsg
	jmp failure1
succes2:
	mov filehandler,ax
	MPrintString offset succesmsg
	jmp mainend
failure2:
	MPrintString offset endl
mainend:		
.EXIT
end
; Open a specified file with dos interrupt	
	PrintString openmsg  
	mov ah,fileopen
		mov al,0		; ax = operation, 
						; 0:read only
						; 1:write only
						; 2:write and read
		lea dx,filename	; dx = startadress of file
	int 21h
	jnc succes1
	PrintString failuremsg
	jmp failure1
succes1:
	mov filehandler,ax
	PrintString succesmsg
failure1:
	PrintString endl

; seek in a specified file
PrintString seekmsg 
mov ah,fileseek
		mov bx,filehandler
		mov al,0		; seek mode
						; 0: offset from begin
						; 2: offset from currentposition
						; 3: offest from end 
		xor cx,cx
		mov dx,3		; distance to move cx:dx
	int 21h
	jnc succes4
	PrintString failuremsg
	jmp failure4
succes4:
	PrintString succesmsg
failure4:
	PrintString endl	
	

; Read from a specified file
PrintString readmsg 
mov ah,fileread
		mov bx,filehandler
		mov cx,3		; number of bytes to read
		lea dx,array	; buffer to contain result
	int 21h
	jnc succes3
	PrintString failuremsg
	jmp failure3
succes3:
	mov filehandler,ax
	PrintString array
failure3:
	PrintString endl
	
; Close a specified file with dos interrupt	
	PrintString closemsg 	
	mov ah,fileclose
		mov bx,filehandler
	int 21h	
	jnc succes2
	PrintString failuremsg
	jmp failure2
succes2:
	mov filehandler,ax
	PrintString succesmsg
failure2:
